package com.ruoyi.accounting.validator;

import com.ruoyi.accounting.mapper.AccClassificationMapper;
import com.ruoyi.accounting.validator.annotation.ExistClassificationId;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 分类id是否存在校验器
 */
public class ExistClassificationIdValidator implements ConstraintValidator<ExistClassificationId, Long> {
    @Autowired
    private AccClassificationMapper accClassificationMapper;

    @Override
    public boolean isValid(Long id, ConstraintValidatorContext context) {
        if (id == null) return true; // 可配合 @NotNull 使用
        return accClassificationMapper.existsById(id) > 0;
    }
}
